<?php
/**
* @package   auto_occasion
* @subpackage annonce
* @author    your name
* @copyright 2011 your name
* @link      http://www.yourwebsite.undefined
* @license    All rights reserved
*/

/**
 * page gestion d'annonce en front
 *
 */
class FoAnnonceGestionCtrl extends jController {
    /**
    * FO : page gestion d'annonce
    */
    function index() {
		
        $rep = $this->getResponse('htmlFo');
        jClasses::inc('publicite~pubSrv');
        jClasses::inc('commun~parametre');

        if (isset($_SESSION['FO_SESS_FORMULE_CHOISIE'])){
            unset($_SESSION['FO_SESS_FORMULE_CHOISIE']);
        }
	
        $rep->menusActifs = frontHtmlResponse::MENU_ANNONCE_GESTION ;
        $rep->breadCrumb  = array(frontHtmlResponse::MENU_ACCUEIL, frontHtmlResponse::MENU_ANNONCE_GESTION) ;

        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/js/pub.js');

        jClasses::inc('commun~common');
			
        //xml pour le header : title, desc, keywords
        $headerContent					= common::readXml(PATH_XML_ANNONCE_GESTION_CONNEXION);
        // change le header de la page en cours
        $rep->title						= isset($headerContent->title)?$headerContent->title:'';
		$headerContent->title			= isset($headerContent->title)?$headerContent->title:'' ;
		$headerContent->keywords		= isset($headerContent->keywords)? $headerContent->keywords:'' ;
		$headerContent->desc			= isset($headerContent->desc)? $headerContent->desc:'' ;
		$keywords						= "<meta name='keywords' content='" . $headerContent->keywords . "'>" ;
		$description					= "<meta name='Description' content='" . $headerContent->desc. "'>" ;
        $rep->addHeadContent($keywords);
        $rep->addHeadContent($description);
        // ---
	
        $rep->bodyTpl = 'annonce~FoAnnonceGestion';
        if($this->param('vendeurCoord')){
            $vendeurCoord				= $this->param('vendeurCoord');
            $rep->body->assign('vendeurCoord', $vendeurCoord);
        }
        //pubHidden et rightContent
        $paramPub = array('p1'=>PUB_RIGHT_1_ID,'p2'=>PUB_RIGHT_2_ID,'p3'=>PUB_RIGHT_3_ID,'p4'=>PUB_RIGHT_4_ID,'p5'=>PUB_RIGHT_5_ID,'p6'=>PUB_RIGHT_6_ID);
        $rep->body->assignZone('oZonePubHidden', 'commun~FoPubHidden',$paramPub);
        

        $rep->body->assignZone('oZoneRightContent', 'commun~FoRightContent');
        //---
       
        return $rep;
    }


    /**
    * FO : page gestion d'annonce : oublie d'identifiant de connexion
    */
    function oublieMdp() {
        jClasses::inc('publicite~pubSrv');
        jClasses::inc('commun~parametre');
        jClasses::inc('commun~common');
        jClasses::inc('annonce~vendeurSrv');
        
        $rep = $this->getResponse('htmlFo');
        //xml pour le header : title, desc, keywords
        $headerContent = common::readXml(PATH_XML_ANNONCE_GESTION_OUBLI_MDP);
        // change le header de la page en cours
        $rep->title					=	isset($headerContent->title)?$headerContent->title:'' ;
		$headerContent->keywords	=	isset($headerContent->keywords)?$headerContent->keywords:'' ;
		$headerContent->desc		=	isset($headerContent->desc)?$headerContent->desc:'' ;
		$keywords					= "<meta name='keywords' content='" . $headerContent->keywords . "'>" ;
		$description				= "<meta name='Description' content='" . $headerContent->desc. "'>" ;
        $rep->addHeadContent($keywords);
        $rep->addHeadContent($description);
        // ---

        $rep->menusActifs = frontHtmlResponse::MENU_ANNONCE_GESTION ;
        $rep->breadCrumb  = array(frontHtmlResponse::MENU_ACCUEIL, frontHtmlResponse::MENU_ANNONCE_GESTION) ;

        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/js/pub.js');

        $rep->bodyTpl = 'annonce~FoAnnonceGestionOblieMdp';

        $mdp      = $this->intParam('mdp', 0);
        $courriel = $this->stringParam('courriel', '');
        $vendeur  = common::init('ao_vendeur');

        // Envoie un mail au vendeur, si le mail existe
        if ($courriel && filter_var($courriel, FILTER_VALIDATE_EMAIL)) {
            $vendeur = vendeurSrv::checkMail($courriel);
            if (sizeof($vendeur)) {
                $tpl = new jTpl();
                $tpl->assign('vendeur', $vendeur);
                $vendeur->message = $tpl->fetch('annonce~mailOublieMdp');

                vendeurSrv::sendMail($vendeur);
            }
        }

        $rep->body->assign('mdp', $mdp);
        //pubHidden et rightContent
        $paramPub = array('p1'=>PUB_RIGHT_1_ID,'p2'=>PUB_RIGHT_2_ID,'p3'=>PUB_RIGHT_3_ID,'p4'=>PUB_RIGHT_4_ID,'p5'=>PUB_RIGHT_5_ID,'p6'=>PUB_RIGHT_6_ID);
        $rep->body->assignZone('oZonePubHidden', 'commun~FoPubHidden',$paramPub);
        

        $rep->body->assignZone('oZoneRightContent', 'commun~FoRightContent');
        //---
       
        return $rep;
    }
    
    /*
     * Authentification pour la gestion 
     * de l'annonce
     */
    function verifierCoordonnees(){
        
        jClasses::inc('commun~communSrv');
        jClasses::inc('annonce~annonceSrv') ;
        
        $vendeurCoord				= new stdClass();
        $vendeurCoord->name			= trim($this->stringParam('name',''));
        $vendeurCoord->birth		= communSrv::toDateSql(trim($this->stringParam('birth','')));
        $vendeurCoord->birth_real	= trim($this->stringParam('birth',''));
        $vendeurCoord->immat		= trim($this->stringParam('immat',''));
        
        
        $nbRes						= annonceSrv::checkCoordonnees($vendeurCoord);
        $rep						= $this->getResponse('redirect');

        if($nbRes == 0){
            $rep->action = 'annonce~FoAnnonceGestion:index';
            $rep->params = array('vendeurCoord' => $vendeurCoord);
        } else {
            $rep->action = 'annonce~FoAnnonceGestion:gererAnnonce';
            $rep->params = array('idOccasion' => $nbRes);
            // session de connexion
			$iAnnonceId	 = $nbRes ;
            $_SESSION['SESS_FO_GESION_ANNONCE'] = $iAnnonceId ;
        }
        
        return $rep;

    }
    
    /*
     * Gestion de l'annonce si authentifié
     */
    function gererAnnonce(){

        $idOccasion = $this->intParam('idOccasion',0);
        // verification de l'authentification
        if ( !isset($_SESSION['SESS_FO_GESION_ANNONCE']) || $_SESSION['SESS_FO_GESION_ANNONCE']!=$idOccasion) {
            $rep         = $this->getResponse('redirect');
            $rep->action = 'annonce~FoAnnonceGestion:index';

            return $rep;
        }

        $rep        = $this->getResponse('htmlFo');

        jClasses::inc('publicite~pubSrv');
        jClasses::inc('commun~parametre');
        jClasses::inc('commun~common');
        jClasses::inc('annonce~annonceSrv');
        jClasses::inc("annonce~annonceFormuleSrv") ;


        $voiture          = common::chargeParId($idOccasion, "ao_voiture_occasion");
        $reference        = $voiture->Occas_Reference;
        $index_parut      = substr($reference, 6, 1);
        $index_photo      = substr($reference, 7, 1);
        $annonceId        = annonceSrv::getAll(1,$idOccasion);
        $url              = annonceSrv::getUrlAnnonce($annonceId);


        $rep->menusActifs = frontHtmlResponse::MENU_ANNONCE_GESTION ;
        $rep->breadCrumb  = array(frontHtmlResponse::MENU_ACCUEIL, frontHtmlResponse::MENU_ANNONCE_GESTION) ;

        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/js/pub.js');


        //xml pour le header : title, desc, keywords
        $headerContent = common::readXml(PATH_XML_ANNONCE_GESTION_ACCUEIL);
        // change le header de la page en cours
        $rep->title					=	isset($headerContent->title)?$headerContent->title:'' ;
		$headerContent->keywords	=	isset($headerContent->keywords)?$headerContent->keywords:'' ;
		$headerContent->desc		=	isset($headerContent->desc)?$headerContent->desc:'' ;
		$keywords					= "<meta name='keywords' content='" . $headerContent->keywords . "'>" ;
		$description				= "<meta name='Description' content='" . $headerContent->desc. "'>" ;
        $rep->addHeadContent($keywords);
        $rep->addHeadContent($description);
        // ---

        if (($voiture->IdOccasion != null) && ($voiture->IdOccasion != '')){
             $annonce            = $voiture;
             $idFormuleSelected  = $annonce->annonce_formule_id;
             if($annonce->annonce_formule_id == 0)
                $idFormuleSelected  = annonceFormuleSrv::getFormuleFromOldDatas($voiture->Occas_Reference);
             
        } else {
            
            $idFormuleSelected  = annonceFormuleSrv::getFormuleFromOldDatas($voiture->Occas_Reference);
            $annonce            = $voiture;
        }
	    $recapAnnonceFormule = annonceFormuleSrv::getFormuleRecap($idFormuleSelected);


        $rep->bodyTpl     = 'annonce~FoAnnonceGestionAccueil';
        $rep->body->assign('idOccasion', $idOccasion);
        $rep->body->assign('voiture', $voiture);
        $rep->body->assign('parution', $index_parut);
        $rep->body->assign('photo', $index_photo);
        $rep->body->assign('annonce', $annonce);
        $rep->body->assign('url', $url);
        $rep->body->assign('recapAnnonceFormule', $recapAnnonceFormule);

        //pubHidden et rightContent
        $paramPub = array('p1'=>PUB_RIGHT_1_ID,'p2'=>PUB_RIGHT_2_ID,'p3'=>PUB_RIGHT_3_ID,'p4'=>PUB_RIGHT_4_ID,'p5'=>PUB_RIGHT_5_ID,'p6'=>PUB_RIGHT_6_ID);
        $rep->body->assignZone('oZonePubHidden', 'commun~FoPubHidden',$paramPub);
        

        $rep->body->assignZone('oZoneRightContent', 'commun~FoRightContent');
        //---
        $rep->body->assignZone('oZoneMenu', 'annonce~FoAnnonceGestionMenu');
       


        return $rep;
    }
    /*
     * FO : Gestion de l'annonce : clic sur modifier
     */
    function edit(){

		$idOccasion = $this->intParam('idOccasion',0);
		// verification de l'authentification
		if ( !isset($_SESSION['SESS_FO_GESION_ANNONCE']) || $_SESSION['SESS_FO_GESION_ANNONCE']!=$idOccasion) {
			$rep         = $this->getResponse('redirect');
			$rep->action = 'annonce~FoAnnonceGestion:index';

			return $rep;
		}

        
        
        jClasses::inc("commun~common");
        jClasses::inc('essais~essaisSrv') ;
        jClasses::inc('marqueModele~marqueSrv') ;
        jClasses::inc('publicite~pubSrv');
        jClasses::inc('commun~parametre');
        jClasses::inc('annonce~annonceSrv');
        jClasses::inc("annonce~annonceCoordonneesSrv");
        
        $voiture            = common::chargeParId($this->intParam('idOccasion'), "ao_voiture_occasion");
        $comment_length = 90 - strlen($voiture->Occas_Commentaire);
        $comment_length = ($comment_length < 0) ? 0 : 90 - strlen($voiture->Occas_Commentaire);
        $vendeur            = common::chargeParId($voiture->Occas_Vendeur, "ao_vendeur");
        $toCatVoitures      = essaisSrv::getAllCatVoiture() ;
        $equipement         = ($voiture->IdOccasion) ? annonceSrv::groupEquipementByCateg(annonceSrv::getEquipementAnnonce($voiture->IdOccasion)) : array();
        $civilite            = annonceCoordonneesSrv::listeCivilite();
        $pays                = annonceCoordonneesSrv::listePays();
        $departement         = annonceCoordonneesSrv::listeDepartement();

        $rep = $this->getResponse('htmlFo');
        

        // Récupération des données correspondant à l'annonce

        
        $rep->menusActifs = frontHtmlResponse::MENU_ANNONCE_GESTION ;
        $rep->breadCrumb  = array(frontHtmlResponse::MENU_ACCUEIL, frontHtmlResponse::MENU_ANNONCE_GESTION) ;

        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/js/pub.js');
        $rep->addCSSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/css/annonceGestion.css');

        //xml pour le header : title, desc, keywords
        $headerContent = common::readXml(PATH_XML_ANNONCE_GESTION_MODIFIER);
        // change le header de la page en cours
        $rep->title					=	isset($headerContent->title)?$headerContent->title:'' ;
		$headerContent->keywords	=	isset($headerContent->keywords)?$headerContent->keywords:'' ;
		$headerContent->desc		=	isset($headerContent->desc)?$headerContent->desc:'' ;
		$keywords					= "<meta name='keywords' content='" . $headerContent->keywords . "'>" ;
		$description				= "<meta name='Description' content='" . $headerContent->desc. "'>" ;
        $rep->addHeadContent($keywords);
        $rep->addHeadContent($description);
        // ---

        $rep->bodyTpl = 'annonce~FoAnnonceGestionEdit';
        
        $rep->body->assign('idOccasion', $idOccasion);
        $rep->body->assign('voiture', $voiture);
        $rep->body->assign('vendeur', $vendeur);
        $rep->body->assign('equipement', $equipement);
        $rep->body->assign('toCatVoitures', $toCatVoitures->fetchAll());
        $rep->body->assign('dateCirculation', date('Y'));
        $rep->body->assign('civilite'    , $civilite);
        $rep->body->assign('pays'        , $pays);
        $rep->body->assign('departement' , $departement);
        $rep->body->assign('civilite'    , $civilite);
        $rep->body->assign('pays'        , $pays);
        $rep->body->assign('departement' , $departement);
        $rep->body->assign('comment_length' , $comment_length);
       
        //pubHidden et rightContent
        $paramPub = array('p1'=>PUB_RIGHT_1_ID,'p2'=>PUB_RIGHT_2_ID,'p3'=>PUB_RIGHT_3_ID,'p4'=>PUB_RIGHT_4_ID,'p5'=>PUB_RIGHT_5_ID,'p6'=>PUB_RIGHT_6_ID);
        $rep->body->assignZone('oZonePubHidden', 'commun~FoPubHidden',$paramPub);
        

        $rep->body->assignZone('oZoneRightContent', 'commun~FoRightContent');
        //---
        $rep->body->assignZone('oZoneMenu', 'annonce~FoAnnonceGestionMenu');

        return $rep;
    }
    /*
     * FO : Gestion de l'annonce : clic sur formule/options
     */
    function formuleOptions(){

        $idOccasion = $this->intParam('idOccasion',0);
		// verification de l'authentification
		if ( !isset($_SESSION['SESS_FO_GESION_ANNONCE']) || $_SESSION['SESS_FO_GESION_ANNONCE']!=$idOccasion) {
			$rep         = $this->getResponse('redirect');
			$rep->action = 'annonce~FoAnnonceGestion:index';

			return $rep;
		}
       
		//Inclusion des classes services
        jClasses::inc("commun~common");
        jClasses::inc('essais~essaisSrv') ;
        jClasses::inc('marqueModele~marqueSrv') ;
        jClasses::inc('publicite~pubSrv');
        jClasses::inc('commun~parametre');
        jClasses::inc('annonce~annonceSrv');
        jClasses::inc("annonce~annonceCoordonneesSrv");
        jClasses::inc("annonce~annonceOrigineSrv");
        jClasses::inc("annonce~annonceGarantieSrv");
        jClasses::inc("annonce~annonceFormuleSrv") ;
        jClasses::inc("formule~formuleSrv");
        jClasses::inc("formule~formuleOptionSrv");
        jClasses::inc('temoignage~temoignageSrv');
        jClasses::inc('annonce~annoncePaiementSrv');

        $rep        = $this->getResponse('htmlFo');
        $err        = $this->intParam('err',0);
        $NUMERR     = $this->intParam('NUMERR',0);

        
	    $rep->menusActifs = frontHtmlResponse::MENU_ANNONCE_GESTION ;
        $rep->breadCrumb  = array(frontHtmlResponse::MENU_ACCUEIL, frontHtmlResponse::MENU_ANNONCE_GESTION) ;

        //Inclusion des javasripts et css
        $rep->addCSSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/css/annonceGestion.css');
        $rep->addCSSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/css/annonceChoixFormule.css');
        $rep->addCSSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/css/jquery-ui.css');
		$rep->addCSSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/css/ui.multiselect.css');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/jquery-ui.min.js');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/js/pub.js');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/js/choixFormuleGestion.js');

        $voiture   = common::chargeParId($this->intParam('idOccasion'), "ao_voiture_occasion");
        $reglement = common::liste('ao_option_typepaiement');
		
		$zChoix			= $this->Param('choix','');
		$iFormuleId		= $this->Param('formuleId','');
		if( isset($zChoix) && $zChoix!='' ){
				//Si l'annonce existe dèja et la formule existe aussi on fait une mise à jour dans la table ao_annonce_formule
				$zListesOptions	= annonceFormuleSrv::getAllOptionsByFormuleAndAnnonce($iFormuleId ,$idOccasion) ;
				//Creation de nouveau choix 
				$zNouveauOptions= $zListesOptions . $zChoix ;
				//on fait une mise à jour de la correspondance entre annonce/formule/otion
				$bSuccess		= annonceFormuleSrv::modifierOptionsSuplementaire($iFormuleId,$zNouveauOptions,$idOccasion) ;
				//On fait une redirectin vers la page de remerciement
				$rep         = $this->getResponse('redirect');
				$rep->action = 'annonce~FoAnnoncePaiement:merci';
		}

        //recuperer la formule si liaison entre annonce-formule ou ancienne donnée
        if (($voiture->IdOccasion != null) && ($voiture->IdOccasion != '')){
             $annonce            = $voiture;
             $idFormuleSelected  = $annonce->annonce_formule_id;
             if($annonce->annonce_formule_id == 0)
			 {
                $idFormuleSelected  = annonceFormuleSrv::getFormuleFromOldDatas($voiture->Occas_Reference);
			 }
             
        } else {
            
            $idFormuleSelected  = annonceFormuleSrv::getFormuleFromOldDatas($voiture->Occas_Reference);
            $annonce            = $voiture;
        }

        $formuleSelected		= common::chargeParId($idFormuleSelected, "ao_formule");
        $infosFormule			= annonceFormuleSrv::getInfosFormule($idFormuleSelected);
        
        $formule				= annonceFormuleSrv::getFormule();

        $choixFormules			= annonceFormuleSrv::getFormulesChoices($formuleSelected->id);
        $optPrixSup				= annonceFormuleSrv::getOptionsPrixSup($choixFormules,$this->intParam('idOccasion'));
        $tabChoixSelected		= explode(',',annonceFormuleSrv::getAnnonceChoix($this->intParam('idOccasion')));
        $recapAnnonceFormule	= annonceFormuleSrv::getFormuleRecap($idFormuleSelected);
        $recapAnnonceChoix		= annonceFormuleSrv::getAnnonceRecap($idOccasion);

       //informations utiles pour le paiement CB 
		// $patherror Url necessaire pour faire la redirection en cas d'erreur sur le payement
		// L'idée c'est de se rediriger vers la page d'acceui en envoyant un parrametre NUMERR
		// et lui fait un controle si cette parametre est non null et faire une redirection vers le module annnce controlleur FoAnnonce action formuleOptions
		// 'http://' . $_SERVER['SERVER_NAME'] .jUrl::get('annonce~FoAnnonceGestion:formuleOptions', array('idOccasion'=>$idOccasion, 'err'=>1));
		//variable necessaire pour stocker $idOccasion
		$_SESSION['SESS_FO_CURRENT_OCCASION']	= $idOccasion ;
        $patherror								= LINK_PATH_ERROR_PAYEMENT  ;
        $pathregret								= 'http://' . $_SERVER['SERVER_NAME'] . jUrl::get('annonce~FoAnnoncePaiement:regret');
        $urlRetourDefaut						= 'http://' . $_SERVER['SERVER_NAME'] . jUrl::get('annonce~FoAnnonceGestion:formuleOptions', array('idOccasion'=>$idOccasion));
		$zPathPayementSuccess					= 'http://' . $_SERVER['SERVER_NAME'] . jUrl::get('annonce~FoAnnoncePaiement:merci');
        $vendeur								= annoncePaiementSrv::getVendeur($idOccasion);
        $codeErr								= '';
        // si code d'erreur
        if ($err && $NUMERR != 0) {
            $codeErr							= annoncePaiementSrv::getErrorMessage ($NUMERR);
        }
        // Récupération des données correspondant à l'annonce
         
        $rep->bodyTpl							= 'annonce~FoAnnonceGestionFormuleOption';
        $rep->body->assign('infosFormule', $infosFormule);
        $rep->body->assign('idOccasion', $idOccasion);
        $rep->body->assign('tabChoixSelected', $tabChoixSelected);
        $rep->body->assign('annonce', $annonce);
        $rep->body->assign('optPrixSup', $optPrixSup);
        $rep->body->assign('formuleSelected', $formuleSelected);
        $rep->body->assign('recapAnnonceFormule', $recapAnnonceFormule);
        $rep->body->assign('recapAnnonceChoix', $recapAnnonceChoix);
        $rep->body->assign('reglement', $reglement);
        $rep->body->assign('vendeur', $vendeur);
        $rep->body->assign('formule', $formule);
        $rep->body->assign('pathregret', $pathregret);
        $rep->body->assign('patherror', $patherror);
        $rep->body->assign('zPathPayementSuccess', $zPathPayementSuccess);
        $rep->body->assign('urlRetourDefaut', $urlRetourDefaut);
        $rep->body->assign('err', $err);
        $rep->body->assign('codeErr', $codeErr);
        //xml pour le header : title, desc, keywords
        $headerContent				= common::readXml(PATH_XML_ANNONCE_GESTION_FORMULE_OPTION);
        // change le header de la page en cours
        $rep->title					=	isset($headerContent->title)?$headerContent->title:'' ;
		$headerContent->keywords	=	isset($headerContent->keywords)?$headerContent->keywords:'' ;
		$headerContent->desc		=	isset($headerContent->desc)?$headerContent->desc:'' ;
		$keywords					= "<meta name='keywords' content='" . $headerContent->keywords . "'>" ;
		$description				= "<meta name='Description' content='" . $headerContent->desc. "'>" ;
        $rep->addHeadContent($keywords);
        $rep->addHeadContent($description);
        // ---    

        return $rep;
    }
    
	/**
	* Cette fonction permet  de mettre à jour la correspondance entre annoce/formule/options
	* @param empty
	* @return $rep
	*/ 
    function saveFormuleOptionsEdited(){

        //paramètres venant de paiement par CB
        $idOcc					= $this->intParam('idOcc', 0) ;
        $idOccasion	 = ($idOcc) ? $idOcc : $this->intParam('idOccasion') ;
		// verification de l'authentification
		if ( !isset($_SESSION['SESS_FO_GESION_ANNONCE']) || $_SESSION['SESS_FO_GESION_ANNONCE']!=$idOccasion) {
			$rep         = $this->getResponse('redirect');
			$rep->action = 'annonce~FoAnnonceGestion:index';

			return $rep;
		}

        jClasses::inc("commun~common");
        jClasses::inc('essais~essaisSrv') ;
        jClasses::inc('marqueModele~marqueSrv') ;
        jClasses::inc('publicite~pubSrv');
        jClasses::inc('commun~parametre');
        jClasses::inc('annonce~annonceSrv');
        jClasses::inc("annonce~annonceCoordonneesSrv");
        jClasses::inc("annonce~annonceOrigineSrv");
        jClasses::inc("annonce~annonceGarantieSrv");
        jClasses::inc("annonce~annonceFormuleSrv") ;
        jClasses::inc("formule~formuleSrv");
        jClasses::inc("formule~formuleOptionSrv");
        jClasses::inc('temoignage~temoignageSrv');


        
        $cf						= $this->intParam('cf', 0) ;

        //traitement sauvegarde
        $formuleId				= ($cf)? $cf        : $this->intParam('choix-form') ;
        $choixSelected			= $this->param('choix') ;
        $typePayement			= $this->intParam('typePayement', 1) ;

        // check changement et save
        annonceSrv::checkFormuleChoicesChange($idOccasion, $formuleId, $choixSelected);

        $optParution			= annonceFormuleSrv::generateOptParutionByFormuleId($formuleId);
        $optParution			= $optParution['option'];
        
        $formuleSelected		= common::chargeParId($formuleId, 'ao_formule');
        
		//Vérification si modification de formule et de choix options supp
		//calcul et mise à jour prix de l'annonce
        if ($choixSelected){
            $tabPrix			= formuleSrv::getPrixOptionChoisie($choixSelected);
            $prixAnnonceChoix	= $tabPrix[0]->prix_option;
        }

        $prixAnnonce			= $formuleSelected->prix + $tabPrix[0]->prix_option ;

	   // mise à jour liaison annonce-formule-choix/option
       annonceSrv::updateOptParution($idOccasion, $optParution);
       annonceFormuleSrv::linkAnnonceFormuleChoix($idOccasion, $formuleSelected->id, $choixSelected);
       //MAJ Occas_MontantPaye après validation
       if($prixAnnonce){
            annonceSrv::updateOccasPaiement($idOccasion,'cb', $prixAnnonce);
       }

       // MAJ de Occas_Estpaye = 1 après validation paiement
        annonceSrv::updatePaiement($idOccasion);

        //fin traitement sauvegarde
        
        $rep					= $this->getResponse('redirect');
        $rep->action			= 'annonce~FoAnnonceGestion:formuleOptions';
        $rep->params			= array('idOccasion'=>$idOccasion);

        return $rep;
    }
    
     /**
     * Get les options
     *
     * @return
     */
    function ajaxGetChoicesOptionsByFormuleId () {
        
        $formuleId  = $this->intParam('formuleId') ;
        $idOccasion = $this->intParam('idOccasion') ;
        
        jClasses::inc('annonce~annonceFormuleSrv') ;
        
        $rep = $this->getResponse('json') ;
        $choixFormules = annonceFormuleSrv::getFormulesChoices($formuleId);
        $optPrixSup    = annonceFormuleSrv::getOptionsPrixSup($choixFormules,$idOccasion);
        $optPrixSup->tabChoixId = explode(',',annonceFormuleSrv::getAnnonceChoix($idOccasion));
        $optPrixSup->formuleId = $formuleId;
        $optPrixSup->recapAnnonceFormule = annonceFormuleSrv::getFormuleRecap($formuleId);
        $rep->data = $optPrixSup;
        
        return $rep;
    }
    
    /*
     * FO : enregistrer
     */
    function enregistrer(){
        jClasses::inc("commun~common");
        jClasses::inc("commun~communSrv");
        jClasses::inc("annonce~annonceSrv");
        
        $annonce                             = new stdClass();
        $annonce->idOccasion                 = $this->intParam('idOccasion',0);
        $voitureSelected = common::chargeParId($annonce->idOccasion, "ao_voiture_occasion");
        $annonce->annonce_id                 = ($voitureSelected->IdOccasion != null ) ? $voitureSelected->IdOccasion : 0;
        $annonce->vendeur_id                 = ($voitureSelected->Occas_Vendeur) ? $voitureSelected->Occas_Vendeur : 0;
        $annonce->annonce_email              = trim($this->stringParam('email',''));
        /* modif vendeur */
        $annonce->vendeur_civilite           = trim($this->stringParam('civilite',''));
        $annonce->vendeur_nom                = trim($this->stringParam('nom',''));
        $annonce->vendeur_prenom             = trim($this->stringParam('prenom',''));
        $annonce->vendeur_naissance          = communSrv::toDateSql(trim($this->stringParam('date_naissance','')));
        $annonce->vendeur_adresse            = trim($this->stringParam('adresse',''));
        $annonce->vendeur_cp                 = trim($this->stringParam('cp',''));
        $annonce->vendeur_ville              = trim($this->stringParam('ville',''));
        $annonce->vendeur_transfert          = trim($this->stringParam('transfert',''));
        $annonce->vendeur_tel_dom            = trim($this->stringParam('domicile',''));
        $annonce->vendeur_tel_bureau         = trim($this->stringParam('bureau',''));
        $annonce->vendeur_tel_por            = trim($this->stringParam('portable',''));
        $annonce->vendeur_email              = trim($this->stringParam('email',''));
        /*fin modif vendeur*/
        
        $annonce->vendeur_pays               = trim($this->stringParam('pays',''));
        $annonce->vendeur_departement        = trim($this->stringParam('departement',''));
        $annonce->vendeur_immatriculation    = trim($this->stringParam('immatriculation',''));
        // param suplémentaire (partenaire et caracteristique du véhicule)
        $annonce->Occas_Marque              = trim($this->intParam('ess_iMarqueId',''));
        $annonce->Occas_Modele              = trim($this->intParam('ess_iModelId',''));
        /* deb modif véhicule*/
        $annonce->Occas_Groupe              = trim($this->intParam('ess_iCatId',''));
        $annonce->Occas_Version             = trim($this->stringParam('ess_zVersion',''));
        $annonce->Occas_Carburation         = trim($this->intParam('voiture_energie',''));
        $annonce->Occas_Prix                = trim($this->intParam('voiture_prix',''));
        $annonce->Occas_Adebattre           = trim($this->intParam('voiture_debattre',''));
        $moisCirculation                    = trim($this->stringParam('circulation_1',''));
        $anneeCirculation                   = trim($this->stringParam('circulation_2',''));

        $annonce->Occas_MiseCirculation = ($moisCirculation && $anneeCirculation) ? $anneeCirculation . '-' . $moisCirculation . '-01' : '0000-00-00';
        $annonce->Occas_Millesime       = trim($this->stringParam('millesime',''));
        $annonce->Occas_Kilometrage     = trim($this->stringParam('km',''));
        $annonce->Occas_Kilom_Moteur    = trim($this->stringParam('km-moteur',''));
        $annonce->Occas_Couleur         = trim($this->stringParam('couleur',''));
        $annonce->Occas_Coul_Metal      = trim($this->intParam('metal',''));
        $annonce->Occas_ChevFisc        = trim($this->intParam('puiss-fiscal',''));
        $annonce->Occas_NbPorte         = trim($this->intParam('nb-porte',''));
        $annonce->Occas_Bt_Vitesse      = trim($this->intParam('boite',''));
        $annonce->Occas_Premieremain    = trim($this->intParam('main',''));
        $annonce->Occas_CT              = trim($this->stringParam('ctrl-tech',''));
        $annonce->Occas_Commentaire     = trim($this->stringParam('comment',''));
        
        /*fin modif véhicule*/
        
        $annonce->Occas_IDpartenaire    = trim($this->intParam('partenaire',''));
        
        $annonce->Occas_FImage          = $voitureSelected->Occas_Fimage;

        $moisGarantie  = trim($this->stringParam('garantie_1',''));
        $anneeGarantie = trim($this->stringParam('garantie_2',''));
        
        /*enregistrer champs modifiés*/
        annonceSrv::checkModif($voitureSelected, $annonce->vendeur_id , $annonce);
        
        $annonce->Occas_Garantie = ($moisGarantie && $anneeGarantie) ? $anneeGarantie . '-' . $moisGarantie . '-01' : '0000-00-00';
        
        $annonce->vendeur_id = annonceSrv::getVendeurId($annonce);
        $annonce->idOccasion = annonceSrv::insertOrUpdateVoitureOccasion($annonce);
        
        $rep         = $this->getResponse('redirect');
        $rep->action = 'annonce~FoAnnonceGestion:gererAnnonce';
        $rep->params = array('idOccasion' => $annonce->idOccasion);

        return $rep;
        
    }
    /*
     * FO : Gestion de l'annonce : clic sur photos
     */
    function photos(){
        
        $rep = $this->getResponse('htmlFo');
         // verification de l'authentification
        if (!isset($_SESSION['SESS_FO_GESION_ANNONCE'])) {
            $rep         = $this->getResponse('redirect');
            $rep->action = 'annonce~FoAnnonceGestion:index';

            return $rep;
        }


        $idOccasion = $this->intParam('idOccasion',0);

        jClasses::inc('publicite~pubSrv');
        jClasses::inc('commun~parametre');
        jClasses::inc('commun~common');
        jClasses::inc('annonce~annonceSrv');
        jClasses::inc('commun~toolsFile');

       

        $voiture          = common::chargeParId($idOccasion, "ao_voiture_occasion");
        $vendeur          = common::chargeParId($voiture->Occas_Vendeur , "ao_vendeur");
        $photo_princ = '';
        if($voiture->Occas_Fimage <>''){
            $photoOccasion = $voiture->Occas_Fimage;
            $photo =  array();
            $trouve = false;
            $imgPath = PHOTO_ANNONCE_PATH;
            //tester si le repertoire existe
            $dirPath = JELIX_APP_WWW_PATH .PATH_ANNONCE_IMG . $photoOccasion;
            if (is_dir($dirPath)){
                //lister les images dans le repertoire
                if ($dh = opendir($dirPath)) {
                    while (($file = readdir($dh)) !== false) {
                        $filename = $dirPath . '/'. $file;
                        $ext = explode(".", $file);
                        if (file_exists($filename) && isset($ext[1]) && toolsFile::is_image($ext[1])) {
                            // formation de l'objet photo
                            $images = new stdClass();
                            $images->photo_name = PATH_ANNONCE_IMG . $photoOccasion . '/' . $file;
                            $photo[] = $images;
                            $imgPath = "";
                            $trouve = true;
                        }
                    }
                    closedir($dh);
                }
            }
            if($trouve) {
                $photo_princ = $photo[0]->photo_name;
                $rep->body->assign('photoOccasion', $photo[0]->photo_name);
                $rep->body->assign('fimage', 1);
            }
           
        }
        $photoSrc         = annonceSrv::getPhotoOccasion($idOccasion);
        if(($photo_princ == '') && (sizeof($photoSrc) > 0)){
            $photo_princ = $photoSrc[0]->photo_name;
        }
        
        $rep->menusActifs = frontHtmlResponse::MENU_ANNONCE_GESTION_PHOTO ;
        $rep->breadCrumb  = array(frontHtmlResponse::MENU_ACCUEIL, frontHtmlResponse::MENU_ANNONCE_GESTION_PHOTO) ;

        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/js/pub.js');


        $rep->addCSSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/css/jquery-ui.css');
		$rep->addCSSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/css/fileuploader.css');
        $rep->addCSSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/prettyPhoto/css/prettyPhoto.css');
        $rep->addCSSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/css/infiniteCarousel.css');
        $rep->addCSSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/css/annonceDeposer.css');
        $rep->addCSSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/css/photoUpload.css');
        


        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/prettyPhoto/js/jquery.prettyPhoto.js');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/js/infiniteCarousel.js');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/ajaxupload.3.5.js');
        
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/js/photoUpload.js');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/js/controle.js');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/js/annonceDeposer.js');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/js/accordion.js');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/js/jquery.iframe.js');
        
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/jquery-ui.min.js');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/jquery.iframe.js');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/jquery.opacityrollover.js');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/jquery.history.js');
        


        //xml pour le header : title, desc, keywords
        $headerContent = common::readXml(PATH_XML_ANNONCE_GESTION_PHOTO);
        // change le header de la page en cours
        $rep->title					=	isset($headerContent->title)?$headerContent->title:'' ;
		$headerContent->keywords	=	isset($headerContent->keywords)?$headerContent->keywords:'' ;
		$headerContent->desc		=	isset($headerContent->desc)?$headerContent->desc:'' ;
		$keywords					= "<meta name='keywords' content='" . $headerContent->keywords . "'>" ;
		$description				= "<meta name='Description' content='" . $headerContent->desc. "'>" ;
        $rep->addHeadContent($keywords);
        $rep->addHeadContent($description);
        // ---

        $rep->bodyTpl = 'annonce~FoAnnonceGestionPhotos';

        $rep->body->assign('idOccasion', $idOccasion);
        $rep->body->assign('vendeur', $vendeur);
        $rep->body->assign('photoSrc', $photoSrc);
        $rep->body->assign('photo_princ', $photo_princ);
        
//pubHidden et rightContent
        $paramPub = array('p1'=>PUB_RIGHT_1_ID,'p2'=>PUB_RIGHT_2_ID,'p3'=>PUB_RIGHT_3_ID,'p4'=>PUB_RIGHT_4_ID,'p5'=>PUB_RIGHT_5_ID,'p6'=>PUB_RIGHT_6_ID);
        $rep->body->assignZone('oZonePubHidden', 'commun~FoPubHidden',$paramPub);
        
        
        $rep->body->assignZone('oZoneRightContent', 'commun~FoRightContent');
        //---
        
       
        $rep->body->assignZone('oZoneMenu', 'annonce~FoAnnonceGestionMenu');

        return $rep;
    }
    /*
     * FO : Gestion de l'annonce : clic sur visualiser
     */
    function visualiser(){

        $idOccasion = $this->intParam('idOccasion',0);
		// verification de l'authentification
		if ( !isset($_SESSION['SESS_FO_GESION_ANNONCE']) || $_SESSION['SESS_FO_GESION_ANNONCE']!=$idOccasion) {
			$rep         = $this->getResponse('redirect');
			$rep->action = 'annonce~FoAnnonceGestion:index';

			return $rep;
		}

        jClasses::inc('publicite~pubSrv');
        jClasses::inc('commun~parametre');
        jClasses::inc("annonce~annonceSrv");
        jClasses::inc("essais~essaisSrv") ;
        jClasses::inc('commun~toolsFile');
        jClasses::inc("commun~GetCaptcha") ;
        jClasses::inc("accueil~GoogleMapAPI");

        
            $annonce = annonceSrv::getAll(1, $idOccasion);
            // récupération des photos
            $photo = ($annonce[0]->IdOccasion) ? annonceSrv::getPhotoAnnonce($annonce[0]->IdOccasion) : array();

            // si pas de photo en base, on essaie de réupérer les images dals le repo : /datas/annonce/$id_annonce
            $imgPath = PHOTO_ANNONCE_PATH;
            if (!sizeof($photo)) {
                //tester si le repertoire existe
                $dirPath = JELIX_APP_WWW_PATH . PATH_ANNONCE_IMG . $idOccasion;
                if (is_dir($dirPath)){
                    //lister les images dans le repertoire
                    if ($dh = opendir($dirPath)) {
                        while (($file = readdir($dh)) !== false) {
                            $filename = $dirPath . '/'. $file;
                            $ext = explode(".", $file);
                            if (file_exists($filename) && isset($ext[1]) && toolsFile::is_image($ext[1])) {
                                // formation de l'objet photo
                                $images = new stdClass();
                                $images->photo_name = PATH_ANNONCE_IMG . $idOccasion . '/' . $file;
                                $photo[] = $images;
                                $imgPath = "";
                            }
                        }
                        closedir($dh);
                    }
                }
            }

		// récupération des équipements
		$equipement = ($annonce[0]->IdOccasion) ? annonceSrv::groupEquipementByCateg(annonceSrv::getEquipementAnnonce($annonce[0]->IdOccasion)) : array();

		// bloc info en plus - liste des essais pour le modele
		$critere = new stdClass();
		$critere->marqueId = $annonce[0]->Occas_Marque;
		$critere->modeleId = $annonce[0]->Occas_Modele;

		$essais = essaisSrv::getEssaiByCritere($critere);

        $rep = $this->getResponse('htmlFo');

        $rep->addCSSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/css/galleriffic.css');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/js/jquery.idTabs.min.js');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/js/jquery.galleriffic.js');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/js/popup.js');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/js/ficheAnnonce.js');
		$rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/js/pub.js');

        $rep->menusActifs = frontHtmlResponse::MENU_ANNONCE_GESTION ;
        $rep->breadCrumb  = array(frontHtmlResponse::MENU_ACCUEIL, frontHtmlResponse::MENU_ANNONCE_GESTION) ;

        jClasses::inc('commun~common');

        //xml pour le header : title, desc, keywords
        $headerContent = common::readXml(PATH_XML_ANNONCE_GESTION_VISUALISER);
        // change le header de la page en cours
        $rep->title					=	isset($headerContent->title)?$headerContent->title:'' ;
		$headerContent->keywords	=	isset($headerContent->keywords)?$headerContent->keywords:'' ;
		$headerContent->desc		=	isset($headerContent->desc)?$headerContent->desc:'' ;
		$keywords					= "<meta name='keywords' content='" . $headerContent->keywords . "'>" ;
		$description				= "<meta name='Description' content='" . $headerContent->desc. "'>" ;
        $rep->addHeadContent($keywords);
        $rep->addHeadContent($description);
        // ---

        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/js/pub.js');
                // si venant du formulare de contact
        $contact = $this->intParam('contact', 0);

        // captcha
        $captchaPath = $GLOBALS['gJConfig']->urlengine["basePath"]. "captcha/temp/" . GetCaptcha::getCaptchaImg () . ".jpg" ;

        $annonce[0]->marque_logo = ($annonce[0]->marque_logo) ? str_replace("media/marques/", "media/marques/fiches/", $annonce[0]->marque_logo): $annonce[0]->marque_logo;
        $annonce[0]->marque_logo_petit = ($annonce[0]->marque_logo) ? str_replace("media/marques/fiches/", "media/marques/fiches/vignettes/", $annonce[0]->marque_logo): $annonce[0]->marque_logo;

        $rep->bodyTpl = 'annonce~FoAnnonceGestionVisualiser';
        $rep->body->assign('idOccasion', $idOccasion);
        $rep->body->assign('annonce', $annonce[0]);
        $rep->body->assign('photo', $photo);
        $rep->body->assign('equipement', $equipement);
        $rep->body->assign('essais', $essais);
        $rep->body->assign('imgPath', $imgPath);
        $rep->body->assign('captchaPath', $captchaPath);
        $rep->body->assign('contact', $contact);

        // google map
        $map = new GoogleMapAPI('map');
		// enter YOUR Google Map Key
		
		$map->setAPIKey(GOOGLE_MAP_API_KEY_DEV);
		//taille de la map
		$map->setHeight("300");
		$map->setWidth("350");
		//on desactive la barre de coté?
		$map->disableSidebar();
		//DesActive les  boutons(map/satellite/hybrid).
		$map->disableTypeControls();
		//Quel est le type de carte par defaut ? (map/satellite/hybrid)
		$map->setMapType('map'); // default
		$map->setZoomLevel(9); // zoom
		//On déssactive les boutons pour afficher la direction d'un point a l'autre
		$map->disableDirections();
		// Permet de definir le zoom automatiquement afin de voir tous les marqueurs d'un coup.
		//$map->enableZoomEncompass();
		//Active la mini map en bas a droite
		$map->enableOverviewControl();
		//permet de définir l'icone sur la map, nous on change pas
		//$map->setMarkerIcon('http://www.canadianwinter.ca/uploads/icon/marker_cw.png','http://www.canadianwinter.ca/uploads/icon/marker_cw_shadow.png',6,20,10,1);
 
        //CREATION DES MARQUEURS
        //on peut egalement utiliser addMarkerByCoords() si on a déj�  la latitude et longitude des points
        //Exemple basique
       // create some map markers
        $map->addMarkerByAddress($annonce[0]->region , $annonce[0]->Vendeur_Prenom . ' ' . $annonce[0]->Vendeur_Nom);
            
        $rep->body->assign('google_map_header',$map->getHeaderJS());
        $rep->body->assign('google_map_js',$map->getMapJS());
        $rep->body->assign('google_map',$map->getMap());
        $check = (isset($_SESSION['SESS_zCaptchaControl'])) ? $_SESSION['SESS_zCaptchaControl'] : "";
        $rep->body->assign('check',$check);


        
        $rep->body->assignZone('oZoneMenu', 'annonce~FoAnnonceGestionMenu');

        return $rep;
    }


    /*
     * FO : Gestion de l'annonce : clic sur relancer
     */
    function relancer(){

        $idOccasion = $this->intParam('idOccasion',0);
		// verification de l'authentification
		if ( !isset($_SESSION['SESS_FO_GESION_ANNONCE']) || $_SESSION['SESS_FO_GESION_ANNONCE']!=$idOccasion) {
			$rep         = $this->getResponse('redirect');
			$rep->action = 'annonce~FoAnnonceGestion:index';

			return $rep;
		}

 

        $rep = $this->getResponse('htmlFo');
        jClasses::inc('publicite~pubSrv');
        jClasses::inc('commun~parametre');
        jClasses::inc('annonce~annonceSrv');


       

        $rep->menusActifs = frontHtmlResponse::MENU_ANNONCE_GESTION ;
        $rep->breadCrumb  = array(frontHtmlResponse::MENU_ACCUEIL, frontHtmlResponse::MENU_ANNONCE_GESTION) ;

        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/js/pub.js');

        jClasses::inc('commun~common');

        //xml pour le header : title, desc, keywords
        $headerContent = common::readXml(PATH_XML_ANNONCE_GESTION_RELANCER);
        // change le header de la page en cours
        $rep->title					=	isset($headerContent->title)?$headerContent->title:'' ;
		$headerContent->keywords	=	isset($headerContent->keywords)?$headerContent->keywords:'' ;
		$headerContent->desc		=	isset($headerContent->desc)?$headerContent->desc:'' ;
		$keywords					= "<meta name='keywords' content='" . $headerContent->keywords . "'>" ;
		$description				= "<meta name='Description' content='" . $headerContent->desc. "'>" ;
        $rep->addHeadContent($keywords);
        $rep->addHeadContent($description);
        // ---

        $rep->bodyTpl = 'annonce~FoAnnonceGestionRelancer';

        // Vérification modifications
        $annonce = annonceSrv::checkChanges ($idOccasion) ;

        //Vérifier les champs modifié
        $annonceToRelance = explode(";", $annonce->Occas_EstModifier) ;
        $query = "Occas_Valide = '0'" ;
        if (sizeof ($annonceToRelance)) {
            if($annonceToRelance[1] == 0 or $annonceToRelance[2] == 0 or $annonceToRelance[9] == 0 or $annonceToRelance[11] == 0 or $annonceToRelance[14] == 0 or $annonceToRelance[15] == 0 or $annonceToRelance[17] == 0){
                
                    //L'annonce est automatique validée
                    $query = " Occas_Valide = '1', Occas_EstModifier = '0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0' " ;
                    
                    if($annonce->Occas_Valide != 1){
                        // Envoi du mail d'avertissement de la validation
                        //-----------------------------------------------
                            annonceSrv::alertmailvalid($idOccasion) ;
                    }
            }
        }
        // Mise à jour de la date de modification
        annonceSrv::updateDateModif ($idOccasion, $query) ;


        $rep->body->assign('idOccasion', $idOccasion);
       
        //pubHidden et rightContent
        $paramPub = array('p1'=>PUB_RIGHT_1_ID,'p2'=>PUB_RIGHT_2_ID,'p3'=>PUB_RIGHT_3_ID,'p4'=>PUB_RIGHT_4_ID,'p5'=>PUB_RIGHT_5_ID,'p6'=>PUB_RIGHT_6_ID);
        $rep->body->assignZone('oZonePubHidden', 'commun~FoPubHidden',$paramPub);
        

        $rep->body->assignZone('oZoneRightContent', 'commun~FoRightContent');
        //---
        $rep->body->assignZone('oZoneMenu', 'annonce~FoAnnonceGestionMenu');



        return $rep;
    }
    /*
     * FO : Gestion de l'annonce : clic sur supprimer
     *
     * Met à jour le champ Occas_Valide à 2
     *
     * Puis proposition de temoignage
     *
     * @ref : SVN_2\petites-annonces-vehicules-d-occasion\gestion\supprimer.php
     */
    function supprimer(){

       $idOccasion = $this->intParam('idOccasion',0);
		// verification de l'authentification
		if ( !isset($_SESSION['SESS_FO_GESION_ANNONCE']) || $_SESSION['SESS_FO_GESION_ANNONCE']!=$idOccasion) {
			$rep         = $this->getResponse('redirect');
			$rep->action = 'annonce~FoAnnonceGestion:index';

			return $rep;
		}

        $rep = $this->getResponse('htmlFo');
        jClasses::inc('annonce~annonceSrv');
        jClasses::inc('publicite~pubSrv');
        jClasses::inc('commun~parametre');


        // mets à jour le champ Occas_Valide
        annonceSrv::updateAnnonceStatus($idOccasion, 3);

        $rep->menusActifs = frontHtmlResponse::MENU_ANNONCE_GESTION ;
        $rep->breadCrumb  = array(frontHtmlResponse::MENU_ACCUEIL, frontHtmlResponse::MENU_ANNONCE_GESTION) ;

        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/js/pub.js');

        jClasses::inc('commun~common');

        //xml pour le header : title, desc, keywords
        $headerContent = common::readXml(PATH_XML_ANNONCE_GESTION_SUPPR);
        // change le header de la page en cours
        $rep->title					=	isset($headerContent->title)?$headerContent->title:'' ;
		$headerContent->keywords	=	isset($headerContent->keywords)?$headerContent->keywords:'' ;
		$headerContent->desc		=	isset($headerContent->desc)?$headerContent->desc:'' ;
		$keywords					= "<meta name='keywords' content='" . $headerContent->keywords . "'>" ;
		$description				= "<meta name='Description' content='" . $headerContent->desc. "'>" ;
        $rep->addHeadContent($keywords);
        $rep->addHeadContent($description);
        // ---

        $rep->bodyTpl = 'annonce~FoAnnonceGestionSupprimer';
        $rep->body->assign('idOccasion', $idOccasion);

        $rep->body->assign('idOccasion', $idOccasion);
       
        //pubHidden et rightContent
        $paramPub = array('p1'=>PUB_RIGHT_1_ID,'p2'=>PUB_RIGHT_2_ID,'p3'=>PUB_RIGHT_3_ID,'p4'=>PUB_RIGHT_4_ID,'p5'=>PUB_RIGHT_5_ID,'p6'=>PUB_RIGHT_6_ID);
        $rep->body->assignZone('oZonePubHidden', 'commun~FoPubHidden',$paramPub);
        

        $rep->body->assignZone('oZoneRightContent', 'commun~FoRightContent');
        //---

        $rep->body->assignZone('oZoneMenu', 'annonce~FoAnnonceGestionMenu');
        return $rep;
    }

    /*
     * FO : Gestion de l'annonce : enregistre un temoignage apres suppression d'une annonce
     *
     * @ref : SVN_2\petites-annonces-vehicules-d-occasion\gestion\temoigner.php
     */
    function saveTemoignage(){

        $idOccasion = $this->intParam('idOccasion',0);
		// verification de l'authentification
		if ( !isset($_SESSION['SESS_FO_GESION_ANNONCE']) || $_SESSION['SESS_FO_GESION_ANNONCE']!=$idOccasion) {
			$rep         = $this->getResponse('redirect');
			$rep->action = 'annonce~FoAnnonceGestion:index';

			return $rep;
		}

        $rep = $this->getResponse('htmlFo');

        jClasses::inc('annonce~annonceSrv');
        jClasses::inc('publicite~pubSrv');
        jClasses::inc('commun~parametre');
        jClasses::inc('commun~common');
        jClasses::inc('temoignage~temoignageSrv');

        
        $temoignageDesc = $this->stringParam('description', '');

        // Récupération du vendeur de l'annonce
        $vendeurDatas = annonceSrv::getAnnonceVendeur($idOccasion);

        // Enregistrement du temoignage
        $temoignage = common::init('ao_temoignage');
        $temoignage->vendeur_nom             = $vendeurDatas->Vendeur_Prenom. " " .substr(strtoupper($vendeurDatas->Vendeur_Nom), 0, 1)."." ;
        $temoignage->temoignage_titre        = " Vendeur" ;
        $temoignage->temoignage_description  = $temoignageDesc ;
        $temoignage->location                = substr($vendeurDatas->Vendeur_CP, 0, 2) ;
        $temoignage->vendeur_id              = $vendeurDatas->IdVendeur;
        $temoignage->date_ajout              = date("Y-m-d");
        $temoignage->statut                  = 0;
        $temoignage->date_validation         = "0000-00-00";
        $temoignage->date_refus              = "0000-00-00";
        $temoignage->date_suppr              = "0000-00-00";

        temoignageSrv::add($temoignage);

        $rep->menusActifs = frontHtmlResponse::MENU_ANNONCE_GESTION ;
        $rep->breadCrumb  = array(frontHtmlResponse::MENU_ACCUEIL, frontHtmlResponse::MENU_ANNONCE_GESTION) ;

        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/js/pub.js');

        $rep->bodyTpl = 'annonce~FoAnnonceGestionConfirmationTemoignage';
        $rep->body->assign('idOccasion', $idOccasion);
       
        //pubHidden et rightContent
        $paramPub = array('p1'=>PUB_RIGHT_1_ID,'p2'=>PUB_RIGHT_2_ID,'p3'=>PUB_RIGHT_3_ID,'p4'=>PUB_RIGHT_4_ID,'p5'=>PUB_RIGHT_5_ID,'p6'=>PUB_RIGHT_6_ID);
        $rep->body->assignZone('oZonePubHidden', 'commun~FoPubHidden',$paramPub);
        

        $rep->body->assignZone('oZoneRightContent', 'commun~FoRightContent');
        //---
        $rep->body->assignZone('oZoneMenu', 'annonce~FoAnnonceGestionMenu');

        return $rep;
    }


    /*
     * FO : Gestion de l'annonce : enregistre les photos
     *
     */
    function savePhotos(){
        
        $idOccasion = $this->intParam('idOccasion',0);
		// verification de l'authentification
		if ( !isset($_SESSION['SESS_FO_GESION_ANNONCE']) || $_SESSION['SESS_FO_GESION_ANNONCE']!=$idOccasion) {
			$rep         = $this->getResponse('redirect');
			$rep->action = 'annonce~FoAnnonceGestion:index';

			return $rep;
		}


        jClasses::inc('annonce~annonceSrv');
        jClasses::inc('publicite~pubSrv');
        jClasses::inc('commun~parametre');
        jClasses::inc('commun~common');

        $voiture          = common::chargeParId($idOccasion, "ao_voiture_occasion");
        $reference        = annonceSrv::genererRef($voiture);

        $annonce                             = new stdClass();
        $annonce->idOccasion                 = $idOccasion;
        $ficPrinc = $this->stringParam('fic_princ');
        //photos
        $tabPhotos = explode("/",$ficPrinc);
        if(sizeOf($tabPhotos) == 1){
            $annonce->annonce_photo      = ($this->stringParam('photo-annonce','') != '') ? $this->stringParam('fic_princ') . ',' .$this->stringParam('photo-annonce','') : $this->stringParam('fic_princ');
        } else{
            
            $exts = explode('.',$tabPhotos[sizeOf($tabPhotos) -1]);
            //copier le fichier et le renommer
            $photoName = $voiture->IdOccasion . '_principal.' . $exts[sizeOf($exts) -1];
            $sourFic = JELIX_APP_WWW_PATH . $this->stringParam('fic_princ');
            $destFic = PHOTO_ANNONCE_PATH . $photoName;
            if (copy($sourFic, $destFic)){
                $annonce->annonce_photo      = ($this->stringParam('photo-annonce','') != '') ? $photoName . ',' .$this->stringParam('photo-annonce','') : $photoName;
                annonceSrv::updateFImage($voiture->IdOccasion);
            } 
        }
        
        $annonce->vendeur_id                 = $this->intParam('vendeur_id',0);
        $annonce->annonce_source_id          = $reference->sourceId;
        $annonce->annonce_provenance_id      = $reference->provId;
        $annonce->annonce_parution_id        = $reference->parutId;
        $annonce->annonce_photo_id           = $reference->photo_id;
        $annonce->annonce_enrichissement_id  = $reference->enrId;
        $annonce->annonce_paiement_id        = $reference->paie;
        $annonce->annonce_origine_id         = $reference->origine;
        $annonce->annonce_garantie_id        = 1;
        $annonce->annonce_email              = '';
        $annonce->annonce_video              = '';
        $annonce->annonce_formule_id         = 0;
        $annonce->annonce_id                 = $voiture->IdOccasion;

        $annonce->annonce_id                 = $idOccasion;

        $annonce_photo = array_unique (explode(",",$annonce->annonce_photo));
        $tabPhotoOld   = annonceSrv::getPhotosIds($annonce->annonce_id);
	    $photo_id      = annonceSrv::returnIdPhotos($annonce_photo,$idOccasion);

        //mettre à jour la correspondance photo_id - annonce_id
         annonceSrv::insertPhotoAnnonce($annonce->annonce_id, $photo_id );
         
         /*
          * vérifier si modif au niveau des photos
          * et enregistrer
          */
         
         sort($phot_id);
         $aTmp = array_intersect($phot_id, $tabPhotoOld);
         if(sizeof($photo_id) != sizeof($aTmp)){
             annonceSrv::markPhotosModif($idOccasion);
         }

        $rep = $this->getResponse('redirect');

        $rep->action = 'annonce~FoAnnonceGestion:photos';
        $rep->params = array('idOccasion' => $idOccasion);

        return $rep;
    }



    /*
     * Affichette pdf
     */
    function affichettePdf() {

        jClasses::inc('commun~common');
        jClasses::inc('commun~LibUtil');
        jClasses::inc('commun~flashcode');

        $idOccasion     = $this->intParam('idOccasion',0);
        $voiture        = common::chargeParId($idOccasion, "ao_voiture_occasion");        
        $vendeur        = common::chargeParId($voiture->Occas_Vendeur, "ao_vendeur");
        $marque         = common::chargeParId ($voiture->Occas_Marque, "ao_marque");
        $modele         = common::chargeParId ($voiture->Occas_modele, "ao_modele");
        $urlFiche       = 'http://' . $_SERVER['SERVER_NAME'] . jUrl::get('recherche~FoFiche:index', array('id'=>$idOccasion));

        $rep = $this->getResponse('tcpdf');
 
        $rep->outputFileName = 'affichette.pdf';
        $rep->doDownload = true;
        //recuperation contenu 
        $tpl			 = new jTpl();
        $data			 = new stdClass();
        $data->marque    = $marque->marque_libelle;
        $data->modele    = $modele->modele_libelle;
        $data->version   = $voiture->Occas_Version;
        $data->nbporte   = $voiture->Occas_NbPorte;
        $data->immat     = LibUtil::toDateFR($voiture->Occas_MiseCirculation);
        $data->km        = $voiture->Occas_Kilometrage;
        $data->prix      = $voiture->Occas_Prix;
        $data->dom       = $vendeur->Vendeur_Tel_Dom;
        $data->port      = $vendeur->Vendeur_Tel_Port;
        $data->bur       = $vendeur->Vendeur_Tel_Bureau;
        $data->flashcode =  flashcode::generate($urlFiche);
        $tpl->assign('data', $data);
        $tpl->assign('PATH_FLASHCODE_IMG', PATH_FLASHCODE_IMG);
        
        $contenu = $tpl->fetch('annonce~downloadAffichette');
        // initialise l'objet tcpdf
        $rep->initPdf();
        $rep->tcpdf->setPrintHeader(false);
        $rep->tcpdf->setDisplayMode("real");
        $rep->tcpdf->AddPage('l');
        $rep->tcpdf->SetTitle('Afichette');
        $rep->tcpdf->writeHTML($contenu, true, false, true, false, '');
        
        return $rep;
        
    }

    /**
     * Cette fonction permet  de renvoyer l'url apres payement en ligne
     * @param empty
     * @return $rep
     */ 
     public function ajaxGetFormuleOptions () {
        $rep = $this->getResponse('json');

        try {
            $idOccasion     = $this->intParam('idOccasion', 0) ;
            $formuleId      = $this->intParam('formuleId', 0) ;
            $choixSelected  = $this->stringParam('choixSelected', '') ;
            $rep->data = 'http://' . $_SERVER['SERVER_NAME'] . jUrl::get("annonce~FoAnnonceGestion:saveFormuleOptionsEdited", array('idOcc'=>$idOccasion, 'cf'=>$formuleId,'choix'=>$choixSelected));

        } catch(exception $e) {
            die($e->getMessage());
        }

        return $rep;
    }

}



